package com.linktel.productionLine.controller;

import com.linktel.productionLine.common.BaseResponse;
import com.linktel.productionLine.common.ErrorCode;
import com.linktel.productionLine.common.ResultUtils;
import com.linktel.productionLine.entity.dto.workSheetPreserve.WorkSheetPreserveDTO;
import com.linktel.productionLine.entity.query.workSheetPreserve.AddWorkSheetPreserveQuery;
import com.linktel.productionLine.entity.query.workSheetPreserve.EditWorkSheetPreserveQuery;
import com.linktel.productionLine.exception.BusinessException;
import com.linktel.productionLine.service.WorkSheetPreserveService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *
 *
 * @Author HuangJiaHui
 * @Date 2023/8/25 9:35
 */
@RestController
@RequestMapping("/worksheet-preserve")
@AllArgsConstructor
public class WorkSheetPreserveController {

    private WorkSheetPreserveService workSheetPreserveService;

    /**
     * 查询所有工单维护信息
     *
     * @return 工单维护信息列表
     */
    @GetMapping
    public BaseResponse<List<WorkSheetPreserveDTO>> listWorkSheetPreserve() {
        return ResultUtils.success(workSheetPreserveService.listWorkSheetPreserve());
    }

    /**
     * 新增工单维护信息
     *
     * @param addWorkSheetPreserveQuery 新增的工单维护信息封装对象
     * @return 是否新增成功
     */
    @PostMapping
    public BaseResponse<Boolean> addWorkSheetPreserve(@RequestBody AddWorkSheetPreserveQuery addWorkSheetPreserveQuery) {
        if (addWorkSheetPreserveQuery == null ) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        return ResultUtils.success(workSheetPreserveService.addWorkSheetPreserve(addWorkSheetPreserveQuery));
    }

    /**
     * 删除工单维护信息
     *
     * @param id 主键id
     * @return 是否删除成功
     */
    @DeleteMapping
    public BaseResponse<Boolean> deleteWorkSheetPreserve(Long currentUserId, Long id) {
        if (id == null || id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        return ResultUtils.success(workSheetPreserveService.deleteWorkSheetPreserve(currentUserId, id));
    }

    /**
     * 修改工单维护信息
     *
     * @param editWorkSheetPreserveQuery 要修改的工单维护信息
     * @return 是否更新成功
     */
    @PutMapping
    public BaseResponse<Boolean> editWorkSheetPreserve(@RequestBody EditWorkSheetPreserveQuery editWorkSheetPreserveQuery) {
        if (editWorkSheetPreserveQuery == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        return ResultUtils.success(workSheetPreserveService.editWorkSheetPreserve(editWorkSheetPreserveQuery));
    }

}
